#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QSettings>

#include <QPen>

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->HS_whiteBalance->setTracking(true);
    ui->HS_exposure->setTracking(true);
    ui->HS_gain->setTracking(true);
    ui->HS_brightness->setTracking(true);
    ui->HS_saturation->setTracking(true);

    connect(ui->btn_open,       &QAbstractButton::clicked, this, &MainWindow::slots_btn_open);
    connect(ui->btn_close,      &QAbstractButton::clicked, this, &MainWindow::slots_btn_close);
    connect(ui->btn_reflash,    &QAbstractButton::clicked, this, &MainWindow::slots_btn_reflash);
    connect(ui->btn_save_img,   &QAbstractButton::clicked, this, &MainWindow::slots_btn_save_img);
    connect(ui->btn_open_img,   &QAbstractButton::clicked, this, &MainWindow::slots_btn_open_img);
    connect(ui->btn_open_video, &QAbstractButton::clicked, this, &MainWindow::slots_btn_open_video);
    connect(ui->btn_save_video, &QAbstractButton::clicked, this, &MainWindow::slots_btn_save_video);

    connect(ui->HS_whiteBalance,        &QAbstractSlider::valueChanged, this, &MainWindow::slots_hs_wb);
    connect(ui->HS_exposure,            &QAbstractSlider::valueChanged, this, &MainWindow::slots_hs_exp);
    connect(ui->HS_gain,                &QAbstractSlider::valueChanged, this, &MainWindow::slots_hs_gain);
    connect(ui->HS_brightness,          &QAbstractSlider::valueChanged, this, &MainWindow::slots_hs_bh);
    connect(ui->HS_saturation,          &QAbstractSlider::valueChanged, this, &MainWindow::slots_hs_st);

    slots_btn_reflash(true);//刷新一次摄像头

    ui->L_img->move(0,0);

    ui->cb_resolution->addItems({"640*480", "800*600", "1024*768"});
    cap.start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slots_btn_open(bool enabled)
{
    int capNum = ui->cb_cap->currentIndex();
    int w = 640;
    int h = 480;

    // int x = ui->cb_resolution->currentIndex();
    QString str = ui->cb_resolution->currentText();
    qDebug()<<"cap out put:"<<str;
    if(str=="640*480"){
        w = 640;
        h = 480;
    }else if(str=="800*600"){
        w = 800;
        h = 600;
    }else if(str=="1024*768"){
        w = 1024;
        h = 768;
    }

    cap.user_cap_open(capNum);
    if(cap.is_user_cap_opened()==false){
        return;
    }
    cap.user_cap_start();

    ui->btn_open->setEnabled(false);
    ui->btn_close->setEnabled(true);
    ui->btn_save_img->setEnabled(true);
    ui->btn_save_video->setEnabled(true);

    // ui->frame->setFixedSize(QSize(cap.user_get(CV_CAP_PROP_FRAME_WIDTH)+10, cap.user_get(CV_CAP_PROP_FRAME_HEIGHT)+10));
    // ui->L_img->setFixedSize(QSize(cap.user_get(CV_CAP_PROP_FRAME_WIDTH), cap.user_get(CV_CAP_PROP_FRAME_HEIGHT)));

    cap.user_set(CV_CAP_PROP_FRAME_WIDTH, w);
    cap.user_set(CV_CAP_PROP_FRAME_HEIGHT, h);

    w = cap.user_get(CV_CAP_PROP_FRAME_WIDTH);
    h = cap.user_get(CV_CAP_PROP_FRAME_HEIGHT);

    // cap.user_set(CV_CAP_PROP_AUTO_EXPOSURE, 4);
    int tmp = cap.user_get(CV_CAP_PROP_EXPOSURE);
    ui->HS_exposure->setValue(tmp);
    ui->L_ex->setText(QString::number(tmp));

    tmp = cap.user_get(CV_CAP_PROP_BRIGHTNESS);
    ui->HS_brightness->setValue(tmp);
    ui->L_BI->setText(QString::number(tmp));

    tmp = cap.user_get(CV_CAP_PROP_GAIN);
    ui->HS_gain->setValue(tmp);
    ui->L_gain->setText(QString::number(tmp));

    tmp = cap.user_get(CV_CAP_PROP_SATURATION);
    ui->HS_saturation->setValue(tmp);
    ui->L_so->setText(QString::number(tmp));

    ui->frame->setFixedSize(QSize(w+10, h+10));
    ui->L_img->setFixedSize(QSize(w, h));

    ui->L_img->move(QPoint(5, 5));

    connect(&cap, &user_capture::user_img_done, this, &MainWindow::MainImgProcessing);
    cap.user_get_cap_parm();

}

void MainWindow::slots_btn_close(bool enabled)
{

    cap.user_cap_close();
    ui->btn_open->setEnabled(true);
    ui->btn_close->setEnabled(false);
    ui->btn_save_img->setEnabled(false);
    ui->btn_save_video->setEnabled(false);

    ui->btn_save_video->setText("开始录像");
    cap.user_cap_save_video_stop();
    disconnect(&cap, &user_capture::user_img_done, this, &MainWindow::MainImgProcessing);
}

void MainWindow::slots_btn_reflash(bool enabled)
{

    ui->cb_cap->clear();

    QList<QString> capInfos = cap.user_cap_scan();

    foreach(QString info , capInfos){
        qDebug()<<info;
        ui->cb_cap->addItem(info);
    }

}

void MainWindow::slots_btn_save_img(void)
{

    qDebug("save img");
    string path = "d:/debug1.jpg";
    if(cap.user_cap_save_img(path)==false){
        qDebug("save err");
    }

}

void MainWindow::slots_btn_open_img(void)
{
    //打开图片文件，选择图片
    QString filename = QFileDialog::getOpenFileName(this,tr("Open Image"),QDir::homePath(),tr("(*.jpg)\n(*.bmp)\n(*.png)"));
    // qDebug()<<"filename:"<<filename;
    if(filename.length()==0){
        return;
    }

    string path = filename.toStdString();
    QPixmap src_img = cap.user_img_open(path);
    ui->L_img->setPixmap(src_img);
    ui->btn_save_img->setEnabled(true);

}

void MainWindow::slots_btn_open_video(void)
{

    if(cap.is_user_recording()){
        ui->btn_save_video->setText("开始录像");
        cap.user_cap_save_video_stop();
    }
    if(cap.is_user_cap_opened()){
        cap.user_cap_close();
        ui->btn_open->setEnabled(true);
        ui->btn_close->setEnabled(false);
    }
    string path = "e:/xxx.avi";
    cap.user_video_open(path);
    ui->btn_save_img->setEnabled(true);
    connect(&cap, &user_capture::user_img_done, this, &MainWindow::MainImgProcessing);

}

void MainWindow::slots_btn_save_video(void)
{

    if(cap.is_user_cap_opened()==false){
        return;
    }
    if(ui->btn_save_video->text()=="停止录像"){
        qDebug("停止录像");
        ui->btn_save_video->setText("开始录像");
        cap.user_cap_save_video_stop();
    }else{
        qDebug("开始录像");
        ui->btn_save_video->setText("停止录像");
        string path = "d:/debugvideo.mp4";
        cap.user_cap_save_video_start(path);
    }
}

void MainWindow::MainImgProcessing(const QPixmap & pi)
{

    //显示到label
    ui->L_img->setPixmap(pi);
}



//=====================================================================
void MainWindow::slots_hs_wb(int v)
{
    qDebug()<<"slots_hs_wb"<<v;
    ui->L_WB->setText(QString::number(v));
}

void MainWindow::slots_hs_exp(int v)
{
    qDebug()<<"slots_hs_exp"<<v;
    ui->L_ex->setText(QString::number(v));
    if(cap.is_user_cap_opened()){
        cap.user_set_exposure(v);
    }
}

void MainWindow::slots_hs_gain(int v)
{
    qDebug()<<"slots_hs_gain"<<v;
    ui->L_gain->setText(QString::number(v));
    if(cap.is_user_cap_opened()){
        cap.user_set_gain(v);
    }
}

void MainWindow::slots_hs_bh(int v)
{
    qDebug()<<"slots_hs_bh"<<v;
    ui->L_BI->setText(QString::number(v));
    if(cap.is_user_cap_opened()){
        cap.user_set_brightness(v);
    }
}

void MainWindow::slots_hs_st(int v)
{
    qDebug()<<"slots_hs_st"<<v;
    ui->L_so->setText(QString::number(v));
    if(cap.is_user_cap_opened()){
        cap.user_set_saturation(v);
    }
}
